<?php

namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\group\Group;
use app\models\group\GroupFans;
use app\models\marketing\MarketingGroup;
use app\models\order\Order;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\redis\Cache;

/**
 * Class GroupController
 * @package app\controllers\h5
 */
class GroupController extends H5Controller
{
    public function actionList()
    {
        $category = intval($this->post('category'));

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $key = Cache::$GROUP . 'cate-' . $category . '-' . $page;
        $cache = Cache::get($key);

        if (!$cache) {
            $ids = ProductAreaList::find()
                ->where(['area_id' => Product::$areaMap['GROUP']])
                ->orderBy(['id' => SORT_DESC])
                ->select(['product_id'])
                ->column();

            $query = Product::find()
                ->where(['status' => Product::$statusMapping['OK']])
                ->andWhere(['id' => $ids]);

            if ($category > 0) {
                $query->andWhere(['category_id' => $category]);
            }

            $totalCount = $query->count();
            $totalPage = ceil($totalCount / $pageSize);

            $list = $query->orderBy(['id' => SORT_DESC])
                ->offset(($page - 1) * $pageSize)
                ->limit($pageSize)
                ->with(['mainImage'])
                ->asArray()
                ->all();

            Cache::set($key, json_encode([
                'list' => $list,
                'totalCount' => $totalCount,
                'totalPage' => $totalPage
            ]));
        } else {
            $data = json_decode($cache, true);
            $list = $data['list'];
            $totalCount = $data['totalCount'];
            $totalPage = $data['totalPage'];
        }

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    // public function checkGroupStatus()
    // {
    //
    // }
    //
    // public function checkGroupExist()
    // {
    //
    // }

    public function actionGroupByProduct()
    {
        $product_id = intval($this->post('product_id'));

        $list = Group::find()
            ->where(['product_id' => $product_id])
            // 0 未支付 1 正在进行 2 已完成
            ->andWhere(['status' => 1])
            ->orderBy(['id' => SORT_DESC])
            // ->limit(20)
            ->with(['user', 'order', 'fans', 'fans.user'])
            ->asArray()
            ->all();

        // foreach ($list as $key => $item) {
        //     // not payment
        //     $status = $item['order']['status'];
        //     if (in_array($status, [Order::$statusMapping['NON_PAYMENT'], Order::$statusMapping['CANCEL']])) {
        //         unset($list[$key]);
        //     }
        // }

        $set = MarketingGroup::getLastOne();

        return $this->successEncode([
            'list' => $list,
            'quantity' => $set->quantity
        ]);
    }

    public function actionFansByGroup()
    {
        $group_id = intval($this->post('group_id'));

        $list = GroupFans::find()
            ->where(['group_id' => $group_id])
            // 1 已支付 0 未支付
            ->andWhere(['status' => 1])
            ->orderBy(['id' => SORT_DESC])
            ->with(['user', 'order'])
            ->asArray()
            ->all();

        // foreach ($list as $key => $item) {
        //     // not payment
        //     // warn: js => Object.keys().length > 0
        //     $status = $item['order']['status'];
        //     if (in_array($status, [Order::$statusMapping['NON_PAYMENT'], Order::$statusMapping['CANCEL']])) {
        //         unset($list[$key]);
        //     }
        // }

        $set = MarketingGroup::getLastOne();

        return $this->successEncode([
            'list' => $list,
            'quantity' => $set->quantity
        ]);
    }
}